<?php

namespace frontend\controllers;

use common\config\TimeFormator;
use Yii;
use common\models\IcoProject;
use common\search\IcoProjectSearch;
use frontend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * IcoProjectController implements the CRUD actions for IcoProject model.
 */
class IcoProjectController extends BaseController
{

    /**
     * Lists all IcoProject models.
     * @return mixed
     */
    public function actionList()
    {
        $this->returnJson = true;
        $searchModel = new IcoProjectSearch();
        $data['IcoProjectSearch'] = Yii::$app->request->queryParams;
        $dataProvider = $searchModel->search($data);
        $list = [];
        foreach($dataProvider->getModels() as $k => $model){
            $list[$k] = $this->formatProject($model);
        }
        return $this->renderPageList($dataProvider,$list);
    }

    /**
     * Lists all IcoProject models.
     * @return mixed
     */
    public function actionDetail($id)
    {
        $this->returnJson = true;
        $model = $this->findModel($id);
        $data = $this->formatProject($model);
        foreach($model->orders as $k=>$v){
            $data['orders'][$k] = $v->getAttributes();
            $data['orders'][$k]['member'] = $v->member->getAttributes();
        }
        $data['cover'] = Yii::$app->params['resourceUrl'] . $model->cover;
        return $this->renderJson(200,'ok', $data);
    }

    /**
     * Finds the IcoProject model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return IcoProject the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = IcoProject::findOne($id)) !== null) {
            $model->start_time = TimeFormator::getFormatDay($model->start_time);
            $model->end_time = TimeFormator::getFormatDay($model->end_time);
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * @param IcoProject $model
     * @return array
     */
    protected function formatProject( IcoProject $model )
    {
        $data = $model->getAttributes();
        $data['cover'] = Yii::$app->params['resourceUrl'] . $model->cover;
        $data['status'] = IcoProject::STATUS_NOT_START;
        $data['address'] = 'xsfdsalfjdskafhkdsagfug';
        $start = $model->start_time;
        $end = $model->end_time;
        if($start>time()&&time()<$end){
            $data['status'] = IcoProject::STATUS_ING;
        }
        if($end<=time()){
            $data['status'] = IcoProject::STATUS_OVER;
            if($model->count==$model->current_count){
                $data['status'] = IcoProject::STATUS_COMPLETE;
            }
        }
        return $data;
    }
}
